package com.zzl.boot.bigevent.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTUtils {
    static private String secret = "zzl";
    public static String getToken(Integer id, String username) {
        Map<String, Object> claim = new HashMap<>();
        claim.put("id", id);
        claim.put("username", username);
        String token= JWT.create()
                .withClaim("user",claim)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000*60*60*48))
                .sign(Algorithm.HMAC256(secret));
        return token;
    }

    public static Map<String, Object> verifyToken(String token) {
        return  JWT.require(Algorithm.HMAC256("zzl"))
                .build().
                verify(token).getClaim("user").asMap();

    }
}
